package com.dyh.controller;

import com.dyh.po.policy;
import com.dyh.service.policyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class policyController {
    @Autowired
    policyService policyservice;
    @RequestMapping(value = "policyList")
    public String policyList(Model model){
        List<policy> policys = policyservice.findAllPolicy();
        System.out.println(policys);
        model.addAttribute("PolicyList", policys);
        return "/policy/policylist";
    }


    @RequestMapping(value = "policyEdit")
    public ModelAndView toUserEdit(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String year = request.getParameter("year");
        policy policy = policyservice.findPolicyByYear(year);
        mv.addObject("Policy",policy);
        mv.setViewName("policy/policyEdit");
        return mv;
    }

    /**
     * 修改政策
     * @param request
     * @return String
     */
    @RequestMapping(value = "policyEditAction")
    public String editPolicy(HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String year = request.getParameter("year");
        String topmoney = request.getParameter("topmoney");
        String toppercent = request.getParameter("toppercent");
        int row = policyservice.editPolicy(year,topmoney,toppercent);
        if(row>0){
            return "redirect:/policyList";
        }else{
            return "policy/policyEdit";
        }
    }


    @RequestMapping(value = "policyAdd")
    public String toPolicyAdd(){
        return "policy/policyAdd";
    }


    @RequestMapping(value = "policyAddAction")
    public String addPolicy(HttpServletRequest request,Model model){
        try {
            request.setCharacterEncoding("UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        String year = request.getParameter("year");
        String topmoney = request.getParameter("topmoney");
        String toppercent = request.getParameter("toppercent");
        int row=policyservice.addPolicy(year,topmoney,toppercent);
        System.out.println("row="+row);
        return "redirect:/policyList";
    }

    /**
     * 删除用户动作
     * @param request
     * @return String
     */
    @RequestMapping(value = "policyDeleteAction")
    public String deletePolicy(HttpServletRequest request){
        String year = request.getParameter("year");
        int row = policyservice.deletePolicy(year);
        return "redirect:/policyList";
    }
}
